package _21_多线程juc.demo02;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.Lock;

public class PC_02 {

}

/**
 * 使用 Lock，await，signal 替换之前的 synchronized，wait，notify
 *
 */
class Data2 {

    private int number = 0;

    Lock lock = new ReentrantLock();
    Condition condition = lock.newCondition();
    // condition.await();
    // condition.signal();

    // +1
    public void increment() throws InterruptedException {
        if (number != 0) {
            // 等待
            condition.await();
        }
        number++;
        System.out.println(Thread.currentThread().getName() + " => " + number);
    }

    // -1
    public void decrement() {
        if (number == 0) {
            // 等待
        }
        number--;
        System.out.println(Thread.currentThread().getName() + " => " + number);
    }
}
